package com.github.benmanes.caffeine.demo.referrence;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.nullness.qual.Nullable;

public class DemoSoftRef {
    static System.Logger logger = System.getLogger(DemoSoftRef.class.getName());

    public static void hello(String[] args) {
        System.out.println("args = " + args);
    }


    public static void main(String... args) throws Exception {
        Cache<String, String> cache = Caffeine.newBuilder()
                .weakKeys()
                .softValues()
                .build();

        String key = "疯狂创客圈";
        String val = cache.getIfPresent(key);
        System.out.println(val);

        //当key不存在时，会立即创建出对象来返回，val 不会为空
        val = cache.get(key, k -> {
            System.out.println("key:" + k);
            return "一个高并发 研究社群";
        });
         System.out.println(val);
        cache.put("name", "疯狂创客圈");
        //获取值
        @Nullable String value = cache.getIfPresent("name");
        System.out.println("value = " + value);

    }

}
